const express = require('express');
const cors = require('cors');

const app = express();
app.use(cors())

app.get('/list', (req, res) => {

  // 当前页和每页多少条
  let {currentPage, perSize} = req.query
  perSize = parseInt(perSize, 10)
  // 数据 页数和条数对应得数据
  let list = []

  // 总数据条数
  let total = 66

  // 总页数
  let pageCount = Math.ceil(total / perSize)

  // 起始索引
  let offset = (currentPage - 1) * perSize

  if(currentPage >= pageCount) {
    perSize = total % perSize
  }

  for(let i = offset; i < offset + perSize; i++) {
    list.push({id: i+1, name: 'cai-'+(i+1)})
  }

  res.json({
    currentPage,
    perSize,
    total,
    pageCount,
    list
  })
})

app.listen(8080, () => {
  console.log('listening on 8080...')
})